/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.fraction;

import com.viewpagerindicator.TabPageIndicator;
import me.panpf.sketch.adapter.pager.ViewArrayPagerAdapter;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.listener.PageChangeListener;
import me.panpf.sketch.sample.widget.page.*;
import me.panpf.sketch.util.Utils;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

import java.util.ArrayList;
import java.util.List;

/**
 * ImageShaperTestFraction
 */
public class ImageShaperTestFraction extends Fraction {
    private static final int DEFAULE_INDEX = 0;
    private static final String[] TAB_TITLES = new String[]{"ROUND_RECT", "CIRCLE", "SHAPE_SIZE"};
    private List<Component> mPageViewList = new ArrayList<>();
    private TabPageIndicator mTabList;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return scatter.parse(ResourceTable.Layout_image_shaper_test_fraction, container, false);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mTabList = (TabPageIndicator) getComponent().findComponentById(ResourceTable.Id_tab_list);

        PageSlider pageSlider = (PageSlider) getComponent().findComponentById(ResourceTable.Id_pager_slider);

        mPageViewList.clear();
        mPageViewList.add(new RoundRectImageShaperTestPageView(getFractionAbility().getContext()));
        mPageViewList.add(new CircleImageShaperTestPageView(getFractionAbility().getContext()));
        mPageViewList.add(new ShapeSizeImageShaperTestPageView(getFractionAbility().getContext()));

        ViewArrayPagerAdapter adapter = new ViewArrayPagerAdapter(mPageViewList, TAB_TITLES);
        pageSlider.setProvider(adapter);
        pageSlider.addPageChangedListener(new PageChangeListener(mTabList));

        mTabList.setTabTextSize(Utils.vp2px(getFractionAbility().getContext(), "12fp"));
        mTabList.setTabMargin(20);
        mTabList.setOrientation(Component.HORIZONTAL);
        mTabList.setCentralScrollMode(true);
        mTabList.setTabTextAlignment(TextAlignment.CENTER);
        mTabList.setTabTextColors(getFractionAbility().getContext().getColor(ResourceTable.Color_color_tab_normal),
                Color.WHITE.getValue());
        mTabList.setSelectedTabIndicatorHeight(0);
        mTabList.setViewPager(pageSlider);
        mTabList.setFixedMode(false);
        // 设置默认选择的页签项
        mTabList.selectTab(mTabList.getTabAt(DEFAULE_INDEX));
    }

}
